package com.wxy.demo.controller;

import cn.hutool.json.JSONUtil;
import com.wxy.demo.dto.IdDto;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 异常测试
 * </p>
 *
 * @author e-Xiaoyuan.Wang
 * @since 2022/11/17 15:10
 */
@RestController
@RequestMapping("/exception")
public class ExceptionController {

    /**
     * query参数错误
     * MethodArgumentTypeMismatchException
     *
     * @param id
     * @return
     */
    @GetMapping("/demo1")
    public String demo1(@RequestParam Integer id) {
        return "success:" + id;
    }

    /**
     * body参数错误
     * HttpMessageNotReadableException
     *
     * @param dto
     * @return
     */
    @PostMapping("/demo2")
    public String demo2(@RequestBody IdDto dto) {
        return "success:" + JSONUtil.toJsonStr(dto);
    }

    /**
     * 缺少请求参数
     * MissingServletRequestParameterException
     *
     * @param name
     * @return
     */
    @GetMapping("/demo3")
    public String demo3(@RequestParam String name) {
        return "success:" + name;
    }

    /**
     * 抛出一个异常
     * @return
     */
    @GetMapping("/demo4")
    public String demo4() {
        throw new RuntimeException("接口异常");
    }
}
